using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using SpringWaterDataAccess;
using Utilities;

namespace sow
{
	/// <summary>
	/// Summary description for EditPartner.
	/// </summary>
	public partial class EditForumPost : System.Web.UI.Page
	{
		protected ForumDataAccess sql;
		protected string sSortFilter = "";
		protected string[] sStatus = new string[] {"All","NULL", "Active", "Approved", "New","Archived", "Top"};
		protected string[] sType = new string[] {"All", "bbs", "blog"};
	
	protected void Page_Load(object sender, System.EventArgs e)
	{
				
		if (sSortFilter == "")
			sSortFilter = "CreateDate Desc";
		
		if (!IsPostBack)
		{
			SQLAccess sql = new SQLAccess();

			DataSet dsUser = new DataSet();
			DataSet dsPartner = new DataSet();
			DataSet dsCategory = new DataSet();

			dsUser = sql.GetUsers();
		
			drpUser.DataSource = dsUser;
			drpUser.DataTextField = "NickName";
			drpUser.DataValueField = "UserID";
			drpUser.DataBind();
			drpUser.Items.Insert(0, "All");
			drpUser.SelectedIndex = 0;

			lstUser.DataSource = dsUser;
			lstUser.DataTextField = "NickName";
			lstUser.DataValueField = "UserID";
			lstUser.DataBind();
			drpUser.SelectedIndex = 0;

			dsPartner = sql.getAllPartners();
		
			drpPartner.DataSource = dsPartner;
			drpPartner.DataTextField = "PartnerCode";
			drpPartner.DataValueField = "PartnerID";
			drpPartner.DataBind();
			drpPartner.Items.Insert(0, "All");
			drpPartner.SelectedIndex = 0;

			lstPartner.DataSource = dsPartner;
			lstPartner.DataTextField = "PartnerCode";
			lstPartner.DataValueField = "PartnerID";
			lstPartner.DataBind();
			drpPartner.SelectedIndex = 0;

			dsCategory = sql.getAllCategories();
			
			lstCategory.DataSource = dsCategory;
			lstCategory.DataTextField = "CategoryName";
			lstCategory.DataValueField = "CategoryID";
			lstCategory.DataBind();
			lstCategory.SelectedIndex = 0;

			drpStatus.DataSource = sStatus;
			drpType.DataSource = sType;
			drpStatus.DataBind();
			drpType.DataBind();

			lstStatus.Items.Insert(0, "NULL");
			lstStatus.Items.Insert(1, "Active");
			lstStatus.Items.Insert(2, "Approved");
			lstStatus.Items.Insert(3, "New");
			lstStatus.Items.Insert(4, "Archived");
			lstStatus.Items.Insert(4, "Top");

			drpType.SelectedIndex = 0;
			drpStatus.SelectedIndex = 0;

			Session["SortDir"] = "ASC";

			GetSource();
			BindData();
			DataGrid1.EditItemIndex = -1;
		}
				
	}

	#region Web Form Designer generated code
	override protected void OnInit(EventArgs e)
{
	//
	// CODEGEN: This call is required by the ASP.NET Web Form Designer.
	//
	InitializeComponent();
	base.OnInit(e);
}
		
	/// <summary>
	/// Required method for Designer support - do not modify
	/// the contents of this method with the code editor.
	/// </summary>
	private void InitializeComponent()
{    
		this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
		this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
		this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
		this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid1_SortCommand);
		this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
		this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);

	}
	#endregion

	protected void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
	{
				
		DataGrid1.EditItemIndex = e.Item.ItemIndex;
		GetSource();
		BindData();			
		lblMessage.Text = "Editing!";
	}

	protected void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
	{
				
		int PostID = int.Parse(((TextBox)e.Item.Cells[2].Controls[0]).Text);
						
		//int CategoryID =int.Parse(((TextBox)e.Item.Cells[3].Controls[0]).Text); 
		//int ParentPostID = int.Parse(((TextBox)e.Item.Cells[4].Controls[0]).Text); 
		string Subject = ((TextBox)e.Item.Cells[3].Controls[0]).Text; 
		//string CreateDate = ((TextBox)e.Item.Cells[6].Controls[0]).Text; 
		//string CreateUser = ((TextBox)e.Item.Cells[7].Controls[0]).Text; 
		string Status = ((TextBox)e.Item.Cells[5].Controls[0]).Text; 
		string Summary = ((TextBox)e.Item.Cells[6].Controls[0]).Text; 
		string Type =((TextBox)e.Item.Cells[7].Controls[0]).Text; 
		//int UserID = int.Parse(((TextBox)e.Item.Cells[11].Controls[0]).Text); 
		//int NumberOfVisits = int.Parse(((TextBox)e.Item.Cells[12].Controls[0]).Text); 
		string PartnerID =((TextBox)e.Item.Cells[8].Controls[0]).Text; 
		string OriginAuthor = ((TextBox)e.Item.Cells[9].Controls[0]).Text; 
		string URL = ((TextBox)e.Item.Cells[10].Controls[0]).Text; 
		string CreateUser = ((TextBox)e.Item.Cells[11].Controls[0]).Text; 
		//string OriginalDate =((TextBox)e.Item.Cells[10].Controls[0]).Text; 
				 

				
		lblMessage.Text = "The record: --" + PostID.ToString() + 
		Subject + Status + Summary + Type + PartnerID.ToString() + OriginAuthor + URL + 
		"-- has been updated!!!<br>";
			

		if (sql == null)
		sql = new ForumDataAccess();

		if (PartnerID !="")
			sql.UpdatePost(PostID, Type, Status, Subject, Summary, int.Parse(PartnerID), OriginAuthor, URL,CreateUser);
		else
			sql.UpdatePost(PostID, Type, Status, Subject, Summary, 0, OriginAuthor, URL,CreateUser);
				

		DataGrid1.EditItemIndex = -1;
		GetSource();
		BindData();
				
				
	}

		protected void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			DataGrid1.EditItemIndex = -1;
			GetSource();
			BindData();
			//DataGrid1.EditItemIndex = -1;
		}

		
		protected void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			int PostID =(int)DataGrid1.DataKeys[(int)e.Item.ItemIndex]; 
			if (sql == null)
				sql = new ForumDataAccess();
			sql.DeletePost(PostID);
			
			lblMessage.Text = "The record of PostID = " + PostID.ToString() + " is deleted! <br>";
			
			DataGrid1.EditItemIndex = -1;
			GetSource();
			BindData();
		}

		private void GetSource()
		{
			sql = new ForumDataAccess();
			DataSet dsPost = sql.getAllPosts();
			DataView vwPost = new DataView(dsPost.Tables[0]);

		
			//Set Filter
			//First check User filter
			string sFilter = "";
			if (drpUser.SelectedIndex > 0)
			{
				int idUser = int.Parse(drpUser.SelectedValue);
				sFilter = "UserID = " + idUser.ToString();
			}

			if (drpPartner.SelectedIndex > 0)
			{
				int idPartner = int.Parse(drpPartner.SelectedValue);
				if (sFilter == "")
					sFilter = "PartnerID = " + idPartner.ToString();
				else
                    sFilter += " And PartnerID = " + idPartner.ToString();
			}

			if (drpStatus.SelectedIndex > 0)
			{
				string sStatus = drpStatus.SelectedItem.ToString();
				string sSQL;
				if (sStatus == "NULL")
					sSQL = "Status <> 'Active' And Status <> 'Approved'";
				else
					sSQL = "Status = '" + sStatus + "'";
				
				if (sFilter == "")
					sFilter = sSQL;
				else
					sFilter += " And " + sSQL;
			}

			if (drpType.SelectedIndex > 0)
			{
				string sType = drpType.SelectedItem.ToString();
							
				if (sFilter == "")
					sFilter = "Type = '" + sType + "'";
				else
					sFilter += " And " + "Type = '" + sType + "'";
			}

			if (sFilter != "")
				vwPost.RowFilter = sFilter;

			//lblMessage.Text = sFilter;

			if (sSortFilter != "")
				vwPost.Sort = sSortFilter;
						
			DataGrid1.DataSource = vwPost;
			DataGrid1.DataKeyField = "PostID";

			
			
		}
		private void BindData()
		{
		
			//if (s == "NULL")
			//	sFilter = "Status = 'Active' And Status = 'Approved';";
			//else if (s == "All")
			
			DataGrid1.DataBind();
		}

		private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			DataGrid1.CurrentPageIndex = e.NewPageIndex;
			GetSource();
			BindData();
		}

		private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
		{
			if (Session["SortDir"].ToString() == "ASC")
				Session["SortDir"] = "DESC";
			else
				Session["SortDir"] = "ASC";

			sSortFilter = e.SortExpression + " " + Session["SortDir"].ToString();
			GetSource();
			BindData();
			
		}

		protected void btnSearch_Click(object sender, System.EventArgs e)
		{
			DataGrid1.CurrentPageIndex = 0;
			GetSource();
			BindData();
		}

		protected void btnAdd_Click(object sender, System.EventArgs e)
		{
					
			string sSubject = txtSubject.Text;
			string sStatus = lstStatus.SelectedItem.ToString();
			if (sStatus == "NULL")
				sStatus = "";
			string sSummary = txtSummary.Text;
			string sBody = txtBody.Text;
			string sType = txtType.Text;
			string sCreateDate = DateTime.Now.ToShortDateString();
			int CategoryID = int.Parse(lstCategory.SelectedValue);
			int UserID = int.Parse(lstUser.SelectedValue);
			string sNickName = lstUser.SelectedItem.ToString();
			string PartnerID = lstPartner.SelectedValue.ToString();
			string sPartner = lstPartner.SelectedItem.ToString();

			string sOriAuthor = txtOriAuthor.Text;
			string sURL = txtURL.Text;

			//string sInsert = "INSERT INTO FORUMPOSTS " +
			//	"(CategoryID, Subject, Body, CreateDate, CreateUser, Status, Summary, Type, UserID, PartnerID, OriginAuthor, URL)" +
			//	" VALUES " + 
			//	"("  + CategoryID + "," + "'" + sSubject + "',"+ "'" + sBody + "'," +
			//	"'" + sCreateDate + "',"+ "'" + sNickName + "',"+ "'" + sStatus + "',"+ "'" + sSummary + "'," +
			//	"'" + sType + "',"+ UserID + ","+ PartnerID + "," + "'" + sOriAuthor + "',"+ "'" + sURL + "')";
			
			//lblMessage.Text = sInsert;
			//DataGrid1.CurrentPageIndex = 0;
			
			string forumID; 

			sql = new ForumDataAccess();
			//sql.InsertPost(sInsert);
			sql.InsertPostWithStatus(0, CategoryID,
            sStatus, sURL, sSubject, sBody, UserID, sSummary, sType, out forumID);
			
			lblMessage.Text = "Category : " + CategoryID.ToString() + " UserID: " + UserID.ToString() +" Forum: " + forumID.ToString();

			GetSource();
			BindData();
		}

		
	}
}
